package a2022.a20230104;

/**
 * @author lenovo
 * @date 2023/1/4
 */
public class a {
    public static void main(String[] args) {
//        System.out.println(maxValue(4,2,6));
        System.out.println(maxValue2(4, 2, 6));

    }

    public static int maxValue(int n, int index, int maxSum) {
        int mv = 1, left = index, right = index, sum = n;
        //总和值大于maxsum
        while (sum <= maxSum) {
            //赋值
            sum += right - left + 1;
            ++mv;
            //左端点左移
            left = left == 0 ? 0 : left - 1;
            //右端点右移
            right = right == n - 1 ? n - 1 : right + 1;
            //判断2边端点是否到头且值是否可以使数组全加1
            if (left == 0 && right == n - 1 && n < maxSum - sum) {

            }
        }
        return mv - 1;
    }

    static int maxValue2(int n, int index, int maxSum) {
        int ans = 1, l = index, r = index, sum = n;
        while (sum <= maxSum) {
            sum += r - l + 1;
            ++ans;
            l = l == 0 ? 0 : l - 1;
            r = r == n - 1 ? n - 1 : r + 1;
            if (l == 0 && r == n - 1 && n < maxSum - sum) {
                return ans + (maxSum - sum) / n;
            }
        }
        return ans - 1;
    }

}

